package demo.hdz.mediacodecdecode.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import java.util.List;

import demo.hdz.mediacodecdecode.R;

/**
 * @author hdz
 * @date 2022/06/23 14:50
 */
public class ListDialog extends DialogFragment implements AdapterView.OnItemClickListener {
    private static final String TAG = "ListDialog";
    private final List<String> data;
    private ListDialogAdapter adapter;
    private int curIndex;
    private OnClickOkListener okListener;

    public ListDialog(List<String> data, int def) {
        this.data = data;
        this.curIndex = def;
    }

    public static void run(FragmentManager manager, List<String> data, int defIndex, OnClickOkListener okListener) {
        ListDialog dialog = new ListDialog(data, defIndex);
        dialog.setOnClickOkListener(okListener);
        dialog.show(manager, TAG);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getDialog() != null) {
            getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
        return inflater.inflate(R.layout.dialog_list, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.initView(view);

        view.findViewById(R.id.btn_ok).setOnClickListener(v -> {
            dismissAllowingStateLoss();
            if (okListener != null) {
                okListener.onClick(v, curIndex);
            }
        });

        view.findViewById(R.id.btn_cancel).setOnClickListener(v -> dismissAllowingStateLoss());
    }

    @Override
    public void onStart() {
        Dialog dialog = getDialog();
        if (dialog != null) {
            //dialog.setCancelable(false);
            Window window = dialog.getWindow();
            if (window != null) {
                window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#55000000")));
                window.setDimAmount(0.8f);

                DisplayMetrics dm = new DisplayMetrics();
                window.getWindowManager().getDefaultDisplay().getMetrics(dm);
                WindowManager.LayoutParams attributes = window.getAttributes();
                window.setAttributes(attributes);

                window.setLayout((int) (dm.widthPixels * 0.5), WindowManager.LayoutParams.WRAP_CONTENT);
            }
        }
        super.onStart();
    }


    private void initView(View view) {
        adapter = new ListDialogAdapter(getContext(), this.data, this.curIndex);
        ListView listView = (ListView) view.findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

        adapter.notifyDataSetChanged();
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        this.curIndex = i;
        adapter.setChecked(i);
        adapter.notifyDataSetChanged();
    }

    private void setOnClickOkListener(OnClickOkListener okListener) {
        this.okListener = okListener;
    }

    public interface OnClickOkListener {
        void onClick(View v, int index);
    }
}
